package o2o.web.actions.postman;

import com.jinyou.utils.common.*;
import com.jinyouapp.common.tools.jms.model.JMSNotify;
import jPush.dao.JPushMessage;
import notice.dao.NoticeType;
import o2o.business.sms.JYSmsSeverBusiness;
import o2o.business.sms.data.SMS_TYPE;
import o2o.cache.agent.CityAgentMemory;
import o2o.cache.agent.PostmanAgentRelationMemory;
import o2o.cache.company.postman.PostTypeMemory;
import o2o.cache.company.postman.PostmanTypeRelationMemory;
import o2o.cache.postman.CityPostmanMemory;
import o2o.cache.shop.ShopMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.cache.wallet.UserWalletMemory;
import o2o.config.dao.UserDaoSql;
import o2o.config.dao.utils.DaoUtils;
import o2o.config.dao.utils.Expression;
import o2o.config.dao.utils.Order;
import o2o.config.dao.utils.QueryCondition;
import o2o.config.jms.config.JMSTypeContent;
import o2o.config.jms.sender.JmsSender;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.agent.CityAgent;
import o2o.dao.PO.agent.PostmanAgentRelation;
import o2o.dao.PO.common.ResultInfo;
import o2o.dao.PO.common.UserType;
import o2o.dao.PO.company.postman.PostTypeInfo;
import o2o.dao.PO.postman.*;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.shop.ShopVerify;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.PO.wallet.UserWallet;
import o2o.dao.VO.common.FileUploadInfo;
import o2o.dao.VO.postman.CityPostmanVerifyVO;
import o2o.postman.model.CityPostman;
import o2o.system.data.SYS_CONFIG_PATH;
import o2o.system.data.SYS_SETTINGS;
import o2o.web.actions.company.SysCityCompanyMan;
import o2o.web.actions.notice.Notice;
import o2o.web.actions.user.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by lmy on 2018/2/3 0003.
 */
@Controller
public class PostManVerifyAction {

    // 提交配送员审核    手机端
    @RequestMapping(method = RequestMethod.POST, value = "/post/verify/add")
    @ResponseBody
    public String shop_add(String token, String sysAppKey, CityPostmanVerify info, FileUploadInfo fileUploadInfo, Integer step) {

        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        String username = user.getUsername();

        if (ValidateUtil.isNotAbsInteger(step)) {
            return AjaxResult.errorResult("输入有误，请稍后再试");
        }
        if (null == info) {
            return AjaxResult.errorResult("请补全信息");
        }
//        CityPostman citypostman = CityPostmanMemory.getInstance().getPostman(info.getUsername());
//        if (null != citypostman) {
//            return AjaxResult.errorResult("当前账号已存在！不允许重复添加！");
//        }

        if (ValidateUtil.isNotNull(username)) {
            QueryCondition query = QueryCondition.getInstance();
            Expression expression = Expression.getInstance();
            expression.addChild("delFlag", 0);//所有未删除的
            expression.addChild("username", username);
            expression.addChild("verifyStatus", POSTMAN_VERIFY_STATUS.ING);
            query.addCondition(expression);
            try {
                int count = UserDaoSql.getInstance().getCount(PojoTables.POSTMAN_VERITY, query);
                if (count > 0) {
                    return AjaxResult.errorResult("您已提交申请，请耐心等待");
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("获取失败，请稍后再试");
            }
        }

        if (null != fileUploadInfo) {
            // 用户头像
            if (null != fileUploadInfo.getImage()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl(filepath);
            }
            // 身份证照正面
            if (null != fileUploadInfo.getImage1()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage1());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl1(filepath);
            }
            // 身份证照反面
            if (null != fileUploadInfo.getImage2()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage2());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl2(filepath);
            }
            // 手持身份证
            if (null != fileUploadInfo.getImage3()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage3());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl3(filepath);
            }
            // 营业执行
            if (null != fileUploadInfo.getImage4()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage4());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl4(filepath);
            }
            // 备用字段5
            if (null != fileUploadInfo.getImage5()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage5());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl5(filepath);
            }
            // 备用字段6
            if (null != fileUploadInfo.getImage6()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage6());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl6(filepath);
            }
            // 备用字段7
            if (null != fileUploadInfo.getImage7()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage7());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl7(filepath);
            }
            // 备用字段8
            if (null != fileUploadInfo.getImage8()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage8());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl8(filepath);
            }
        }

        info.setCreateUser(user.getUsername());
        long time = DateUtils.getCurrTime();
        info.setCreateTime(time);
        info.setDelFlag(0);
        info.setIsVerify(0);
        info.setUsername(username);

        if (null != user) {
            switch (user.getUserType()) {
                case UserType.SHOP_OWNER: {  //如果已经是店主， 那就不允许添加了
                    List<ShopInfo> shopInfo = ShopMemory.getInstance().getShopListByUserName(username);
                    if (null != shopInfo && shopInfo.size() > 0) {
                        return AjaxResult.errorResult("当前账号已是店主，不允许再添加");
                    }
                }
                break;
//                case UserType.USER: {//普通用的话，升级为配送员
//                    User.getInstance().modifyUserType(user.getUsername(), UserType.POSTMAN);
//                    user.setUserType(UserType.POSTMAN);
//                }
//                break;
//                case UserType.POSTMAN: {
//                    return AjaxResult.errorResult("当前身份与与要成为的身份相同，请更换账号");
//                }
//                    case UserType.ADMIN: {//管理员的话看看他有没有店铺
//                        List<ShopInfo> shopInfo = ShopMemory.getInstance().getShopListByUserName(userInfo.getUsername());
//                        if (null != shopInfo && shopInfo.size() > 0) {
//                            return AjaxResult.errorResult("当前店主账号已存在，不允许再添加新店铺");
//                        }
//                    }
//                    break;
            }
        }
//            if (ValidateUtil.isAbsDouble(info.getLat()) && ValidateUtil.isAbsDouble(info.getLng())) {
//                String geocode = Geohash.encode(info.getLat(), info.getLng());
//                Long areaPoint = Geohash.getArea(info.getLat(), info.getLng());
//                info.setGeocode(geocode);
//                info.setAreaPoint(areaPoint);
//            }

        switch (step) {
            case 1: { //保存资质信息 到数据库

                if (ValidateUtil.isID(info.getId())) {       // 退回的审核 传id
                    QueryCondition query = QueryCondition.getInstance();
                    Expression expression = Expression.getInstance();
                    expression.addChild("delFlag", 0);      //所有未删除的
                    expression.addChild("id", info.getId());
                    expression.addChild("verifyStatus", POSTMAN_VERIFY_STATUS.BACK);
                    query.addCondition(expression);
                    try {
                        int count = UserDaoSql.getInstance().getCount(PojoTables.POSTMAN_VERITY, query);
                        // 如果有数据 则证明已经提交过 反之则没有提交
                        if (count > 0) {
                            StringBuffer sb = new StringBuffer();
                            sb.append("update ").append(PojoTables.POSTMAN_VERITY)
                                    .append(" set extra1 = '").append(info.getExtra1())   // 执照所在地的城市 选择的
                                    .append("', licenceDate = ").append(info.getLicenceDate())
                                    .append(" ,licenceType =").append(info.getLicenceType())
                                    .append(" ,imageUrl4='").append(info.getImageUrl4())
                                    .append("' ,licenceName='").append(info.getLicenceName())
                                    .append("' ,licence='").append(info.getLicence())
                                    .append("' ,licenceAddress='").append(info.getLicenceAddress())
//                                    .append("' ,isVerify= 0")
//                                    .append(" ,verifyStatus=").append(POSTMAN_VERIFY_STATUS.SUBMIT)
                                    .append("',updateUser = '").append(user.getUsername())
                                    .append("' where delFlag = 0  and username = '").append(username).append("' and verifyStatus =").append(POSTMAN_VERIFY_STATUS.BACK);
                            try {
                                UserDaoSql.getInstance().executeBySql(sb.toString());
                            } catch (SQLException e) {
                                e.printStackTrace();
                                return AjaxResult.errorResult("新增失败，请稍后再试.");
                            }
                        }
//                        注释掉   modify by lmy 3/12 退回后重新提交
//                        else {
//                            try {
//                                info.setVerifyStatus(POSTMAN_VERIFY_STATUS.SUBMIT);
//                                UserDaoSql.getInstance().insertObj(PojoTables.POSTMAN_VERITY, info);
//                            } catch (SQLException e) {
//                                e.printStackTrace();
//                                return AjaxResult.errorResult("新增失败，请稍后再试.");
//                            }
//                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("获取失败，请稍后再试");
                    }
                } else {

                    //  将postman_verify表中之前的username删除
                    StringBuffer sb2 = new StringBuffer();
                    sb2.append("update ").append(PojoTables.POSTMAN_VERITY)
                            .append(" set delFlag = 1")   // 执照所在地的城市 选择的
                            .append(" ,updateUser = '").append(user.getUsername())
                            .append("' where delFlag = 0  and username = '").append(username).append("' ");
                    try {
                        UserDaoSql.getInstance().executeBySql(sb2.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("新增失败，请稍后再试.");
                    }

                    QueryCondition query = QueryCondition.getInstance();
                    Expression expression = Expression.getInstance();
                    expression.addChild("delFlag", 0);      //所有未删除的
                    expression.addChild("username", username);
                    expression.addChild("verifyStatus", POSTMAN_VERIFY_STATUS.SUBMIT);
                    query.addCondition(expression);
                    try {
                        int count = UserDaoSql.getInstance().getCount(PojoTables.POSTMAN_VERITY, query);
                        // 如果有数据 则证明已经提交过 反之则没有提交
                        if (count > 0) {
                            StringBuffer sb = new StringBuffer();
                            sb.append("update ").append(PojoTables.POSTMAN_VERITY)
                                    .append(" set extra1 = '").append(info.getExtra1())   // 执照所在地的城市 选择的
                                    .append("', licenceDate = ").append(info.getLicenceDate())
                                    .append(" ,licenceType =").append(info.getLicenceType())
                                    .append(" ,imageUrl4='").append(info.getImageUrl4())
                                    .append("' ,licenceName='").append(info.getLicenceName())
                                    .append("' ,licence='").append(info.getLicence())
                                    .append("' ,licenceAddress='").append(info.getLicenceAddress())
                                    .append("',updateUser = '").append(user.getUsername())
                                    .append("' where delFlag = 0  and username = '").append(username).append("' and verifyStatus =").append(POSTMAN_VERIFY_STATUS.SUBMIT);
                            try {
                                UserDaoSql.getInstance().executeBySql(sb.toString());
                            } catch (SQLException e) {
                                e.printStackTrace();
                                return AjaxResult.errorResult("新增失败，请稍后再试.");
                            }
                        } else {
                            try {
                                info.setVerifyStatus(POSTMAN_VERIFY_STATUS.SUBMIT);
                                UserDaoSql.getInstance().insertObj(PojoTables.POSTMAN_VERITY, info);
                            } catch (SQLException e) {
                                e.printStackTrace();
                                return AjaxResult.errorResult("新增失败，请稍后再试.");
                            }
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("获取失败，请稍后再试");
                    }
                }
//                }
//                //查询出刚才插入的数据
//                QueryCondition query = QueryCondition.getInstance();
//                query.addCondition(Expression.getInstance().addChild("delFlag", 0).addChild("createTime", time));//所有未删除的
//                String sql = DaoUtils.getSql(PojoTables.POSTMAN_VERITY, query);
//                CityPostmanVerify citypostVerify = null;
//                try {
//                    citypostVerify = (CityPostmanVerify) UserDaoSql.getInstance().getObject(CityPostmanVerify.class, sql);
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//                if (null != citypostVerify) {
//                    postmanId = citypostVerify.getId();
//                }
            }
            break;
            case 2: {  // 认证资料
                boolean flag = false;
                QueryCondition query = QueryCondition.getInstance();
                Expression expression = Expression.getInstance();
                expression.addChild("delFlag", 0);      //所有未删除的
                expression.addChild("username", username);
                expression.addChild("verifyStatus", POSTMAN_VERIFY_STATUS.BACK);
                query.addCondition(expression);
                try {
                    int count = UserDaoSql.getInstance().getCount(PojoTables.POSTMAN_VERITY, query);
                    if (count > 0) {
                        flag = true;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    return AjaxResult.errorResult("查询失败，请稍后再试");
                }

                if (ValidateUtil.isID(info.getId()) || flag) {    //  退回的
                    StringBuffer sb = new StringBuffer();
                    sb.append("update ").append(PojoTables.POSTMAN_VERITY).append(" set name='").append(info.getName())
                            .append("',sex=").append(info.getSex())
                            .append(",telephone=").append(info.getTelephone())
                            .append(",imageUrl='").append(info.getImageUrl())
                            .append("',ownerID='").append(info.getOwnerID())
                            .append("',imageUrl1='").append(info.getImageUrl1())
                            .append("',imageUrl2='").append(info.getImageUrl2())
                            .append("',imageUrl3='").append(info.getImageUrl3())
                            .append("',IDaddress ='").append(info.getIDaddress())
                            .append("',unitName ='").append(info.getUnitName())
                            .append("',province='").append(info.getProvince())
                            .append("',city='").append(info.getCity())
                            .append("',county='").append(info.getCounty())
                            .append("',verifyStatus=").append(POSTMAN_VERIFY_STATUS.ING)
                            .append(" ,isVerify= 0")
                            .append(" ,extra2='").append(info.getExtra2())    // 技能标签id
                            .append("',updateUser = '").append(user.getUsername())
                            .append("'  where delFlag = 0 and username = '").append(username).append("' and verifyStatus =").append(POSTMAN_VERIFY_STATUS.BACK);
                    try {
                        UserDaoSql.getInstance().executeBySql(sb.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("上传信息失败，请稍后再试");
                    }
                } else {
                    StringBuffer sb = new StringBuffer();
                    sb.append("update ").append(PojoTables.POSTMAN_VERITY).append(" set name='").append(info.getName())
                            .append("',sex=").append(info.getSex())
                            .append(",telephone=").append(info.getTelephone())
                            .append(",imageUrl='").append(info.getImageUrl())
                            .append("',ownerID='").append(info.getOwnerID())
                            .append("',imageUrl1='").append(info.getImageUrl1())
                            .append("',imageUrl2='").append(info.getImageUrl2())
                            .append("',imageUrl3='").append(info.getImageUrl3())
                            .append("',IDaddress ='").append(info.getIDaddress())
                            .append("',unitName ='").append(info.getUnitName())
                            .append("',province='").append(info.getProvince())
                            .append("',city='").append(info.getCity())
                            .append("',county='").append(info.getCounty())
                            .append("',verifyStatus=").append(POSTMAN_VERIFY_STATUS.ING)
                            .append(" ,extra2='").append(info.getExtra2())    // 技能标签id
                            .append("',updateUser = '").append(user.getUsername())
                            .append("'  where delFlag = 0 and username = '").append(username).append("' and verifyStatus =").append(POSTMAN_VERIFY_STATUS.SUBMIT);
                    try {
                        UserDaoSql.getInstance().executeBySql(sb.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("上传信息失败，请稍后再试");
                    }
                }

                // 修改city_postman表
                CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(info.getUsername());
                if (null == cityPostman) {
                    cityPostman = new CityPostman(info.getUsername(), info.getProvince(), info.getCity(), info.getCounty(), "", "", user.getUsername(), time);
                    cityPostman.setIsVerify(2);
                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.CityPostman, cityPostman);
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("添加失败，请稍后再试");
                    }
                    //缓存中新增配送员
                    CityPostmanMemory.getInstance().add(time);
                    try {
                        JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.POSTMAN_INFO_ADD).setLong01(time);
                        JmsSender.setTopicSender(notify);
                    } catch (Exception e) {
                    }
                }
                if (null != cityPostman && cityPostman.getIsVerify() == 0) {
//            StringBuilder sb2 = new StringBuilder();
//            sb2.append("update ").append(PojoTables.CityPostman).append(" set userType =").append(UserType.POSTMAN)
//                    .append(" where username = '").append(cityPostmanVerify.getUsername()).append("' and userType = ").append(UserType.USER);
//            try {
//                UserDaoSql.getInstance().executeBySql(sb2.toString());
//            } catch (SQLException e) {
//                e.printStackTrace();
//                return AjaxResult.errorResult("审核失败，请稍后再试");
//            }
//            //修改配送员的用户类型 缓存
//            UserInfoMemory.getInstance().modifyUserType(cityPostmanVerify.getUsername(), UserType.USER);
                    StringBuilder sb4 = new StringBuilder();
                    sb4.append("update ").append(PojoTables.CityPostman).append(" set isVerify = 2")
                            .append(",updateUser = '").append(user.getUsername())
                            .append("' where username = '").append(info.getUsername()).append("'");
                    try {
                        UserDaoSql.getInstance().executeBySql(sb4.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("审核失败，请稍后再试");
                    }

//            cityPostman.setIsVerify(1);
                    CityPostmanMemory.getInstance().reloadCity(cityPostman.getCity());
                    try {
                        JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.POSTMAN_RELOAD_BY_CITY).setStr01(cityPostman.getCity());
                        JmsSender.setTopicSender(notify);
                    } catch (Exception e) {
                    }
                }

                //修改用户角色
                if (0 == UserType.USER - user.getUserType()) {
                    User.modifyUserType(user.getUsername(), UserType.POSTMAN);
                }
                //推送通知
                Notice.publishNotice2Person(sysAppKey, user.getUsername(), JPushMessage.POSTMAN, NoticeType.system, "提交成功", "您已成功提交的认证，请耐心等待");


            }
        }

        return AjaxResult.successResult();
    }

    // 获取step1锁匠审核信息   手机端
    @RequestMapping(method = RequestMethod.POST, value = "/postman/step1/verify/info")
    @ResponseBody
    public String user_step1_verify_info(String token) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        CityPostmanVerify info = null;

        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * FROM ").append(PojoTables.POSTMAN_VERITY)
                .append(" WHERE username ='").append(user.getUsername()).append("' ")
                .append(" AND verifyStatus = ").append(POSTMAN_VERIFY_STATUS.SUBMIT)
                .append(" AND delFlag = 0")
                .append(" ORDER BY createtime DESC")
                .append(" LIMIT 0,1");
        try {
            info = (CityPostmanVerify) UserDaoSql.getInstance().getObject(CityPostmanVerify.class, sql.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("获取失败，请稍后再试");
        }

        if (null != info) {
            if (ValidateUtil.isNotNull(info.getImageUrl()) && ValidateUtil.isNotNetUrl(info.getImageUrl())) {
                info.setImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl());
            }
            if (ValidateUtil.isNotNull(info.getImageUrl1()) && ValidateUtil.isNotNetUrl(info.getImageUrl1())) {
                info.setImageUrl1(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl1());
            }
            if (ValidateUtil.isNotNull(info.getImageUrl2()) && ValidateUtil.isNotNetUrl(info.getImageUrl2())) {
                info.setImageUrl2(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl2());
            }
            if (ValidateUtil.isNotNull(info.getImageUrl3()) && ValidateUtil.isNotNetUrl(info.getImageUrl3())) {
                info.setImageUrl3(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl3());
            }
            if (ValidateUtil.isNotNull(info.getImageUrl4()) && ValidateUtil.isNotNetUrl(info.getImageUrl4())) {
                info.setImageUrl4(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl4());
            }
            if (ValidateUtil.isNotNull(info.getImageUrl5()) && ValidateUtil.isNotNetUrl(info.getImageUrl5())) {
                info.setImageUrl5(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl5());
            }
            return AjaxResult.successResult(info);
        }
        return AjaxResult.errorResult("未查到数据！");
    }


    // 添加认证（人脸识别版）
    @RequestMapping(method = RequestMethod.POST, value = "/postman/verify/add")
    @ResponseBody
    public String postman_add(String token, String sysAppKey, Integer isAppType, CityPostmanVerify info, FileUploadInfo fileUploadInfo, Integer step) {
        System.out.println("骑手认证车辆id--->" + info.getCarTipId());
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        String username = user.getUsername();
        if (null == info) {
            return AjaxResult.errorResult("请补全信息");
        }
        if ((isAppType == null || isAppType != 1) && ValidateUtil.isNotNull(username)) {
            QueryCondition query = QueryCondition.getInstance();
            Expression expression = Expression.getInstance();
            expression.addChild("delFlag", 0);//所有未删除的
            expression.addChild("username", username);
            expression.addChild("verifyStatus", POSTMAN_VERIFY_STATUS.ING);
            query.addCondition(expression);
            try {
                int count = UserDaoSql.getInstance().getCount(PojoTables.POSTMAN_VERITY, query);
                if (count > 0) {
                    return AjaxResult.errorResult("您已提交申请，请耐心等待");
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("获取失败，请稍后再试");
            }
        }

        if (null != fileUploadInfo) {
            // 用户头像
            if (null != fileUploadInfo.getImage()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl(filepath);
            }
            // 身份证照正面
            if (null != fileUploadInfo.getImage1()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage1());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl1(filepath);
            }
            // 身份证照反面
            if (null != fileUploadInfo.getImage2()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage2());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl2(filepath);
            }
            // 手持身份证
            if (null != fileUploadInfo.getImage3()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage3());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl3(filepath);
            }
            // 营业执行
            if (null != fileUploadInfo.getImage4()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage4());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl4(filepath);
            }
            // 备用字段5
            if (null != fileUploadInfo.getImage5()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage5());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl5(filepath);
            }
            // 备用字段6
            if (null != fileUploadInfo.getImage6()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage6());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl6(filepath);
            }
            // 备用字段7
            if (null != fileUploadInfo.getImage7()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage7());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl7(filepath);
            }
            // 备用字段8
            if (null != fileUploadInfo.getImage8()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage8());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl8(filepath);
            }
            // 备用字段9
            if (null != fileUploadInfo.getImage9()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage9());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl9(filepath);
            }
            // 备用字段10
            if (null != fileUploadInfo.getImage10()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage10());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl10(filepath);
            }
            // 备用字段11
            if (null != fileUploadInfo.getImage11()) {
                String filepath = JYImageUtils.saveImage(SYS_CONFIG_PATH.USER_IMAGE, fileUploadInfo.getImage11());
                if (null == filepath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl11(filepath);
            }
        }
        // todo 一乙再次申请骑手类型
        if (isAppType != null && isAppType == 1) {
            try {
                applyPostManType(username, info);
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.errorResult(e.getMessage());
            }
            return AjaxResult.successResult();
        }
        if (ValidateUtil.isNotAbsInteger(step)) {
            return AjaxResult.errorResult("输入有误，请稍后再试");
        }
        boolean isExactUserType = User.isExactUserType();

        if (isExactUserType) {
            // 店铺也能认证

        } else {
            if (null != user) {
                switch (user.getUserType()) {
                    case UserType.SHOP_OWNER: {  //如果已经是店主， 那就不允许添加了
                        List<ShopInfo> shopInfo = ShopMemory.getInstance().getShopListByUserName(username);
                        if (null != shopInfo && shopInfo.size() > 0) {
                            return AjaxResult.errorResult("当前账号已是店主，不允许再添加");
                        }
                    }
                    break;
                }
            }
        }
        switch (step) {
            case 1: { //第一步 上传身份证信息
                if (null == info.getType()) {
                    info.setType(POSTMAN_TYPE.POSTMAN);
                }
                //身份证图片
                if (ValidateUtil.isNull(info.getImageUrl1())) {
                    return AjaxResult.errorResult("请先上传身份证照片");
                }
                //身份证名称 身份证号
                if (ValidateUtil.isNull(info.getOwner())) {
                    return AjaxResult.errorResult("请填写姓名");
                }
                if (ValidateUtil.isNull(info.getOwnerID())) {
                    return AjaxResult.errorResult("请填写身份证号");
                }
                if (ValidateUtil.isID(info.getId())) {// 退回的审核 传id
                    QueryCondition query = QueryCondition.getInstance();
                    Expression expression = Expression.getInstance();
                    expression.addChild("delFlag", 0);      //所有未删除的
                    expression.addChild("id", info.getId());
                    expression.addChild("verifyStatus", POSTMAN_VERIFY_STATUS.BACK);
                    query.addCondition(expression);
                    try {
                        int count = UserDaoSql.getInstance().getCount(PojoTables.POSTMAN_VERITY, query);
                        // 如果有数据 则证明已经提交过 反之则没有提交
                        if (count > 0) {
                            StringBuffer sb = new StringBuffer();
                            sb.append("update ").append(PojoTables.POSTMAN_VERITY)
                                    .append(" set imageUrl1='").append(info.getImageUrl1()).append("',owner='").append(info.getOwner())
                                    .append("',ownerID='").append(info.getOwnerID()).append("',updateUser = '").append(user.getUsername())
                                    .append("' where delFlag = 0  and username = '").append(username).append("' and verifyStatus =").append(POSTMAN_VERIFY_STATUS.BACK);
                            try {
                                UserDaoSql.getInstance().executeBySql(sb.toString());
                            } catch (SQLException e) {
                                e.printStackTrace();
                                return AjaxResult.errorResult("新增失败，请稍后再试.");
                            }
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("获取失败，请稍后再试");
                    }
                } else {
                    //  将postman_verify表中之前的username删除
                    StringBuffer sb2 = new StringBuffer();
                    if (SYS_SETTINGS.THIRD_PARTY_COMPANY == null || SYS_SETTINGS.THIRD_PARTY_COMPANY == 0) {
                        sb2.append("update ").append(PojoTables.POSTMAN_VERITY)
                                .append(" set delFlag = 1")   // 执照所在地的城市 选择的
                                .append(" ,updateUser = '").append(user.getUsername())
                                .append("' where delFlag = 0  and username = '").append(username).append("'")
                                .append(" and verifyStatus = ").append(POSTMAN_VERIFY_STATUS.SUBMIT);
                    } else {
                        sb2.append("update ").append(PojoTables.POSTMAN_VERITY)
                                .append(" set delFlag = 1")   // 执照所在地的城市 选择的
                                .append(" ,updateUser = '").append(user.getUsername())
                                .append("' where delFlag = 0  and username = '").append(username).append("'")
                                .append(" and verifyStatus = ").append(POSTMAN_VERIFY_STATUS.SUBMIT);
                    }

                    try {
                        UserDaoSql.getInstance().executeBySql(sb2.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("新增失败，请稍后再试.");
                    }
                    CityPostmanVerify infoDB = null;

                    StringBuilder sql = new StringBuilder();
                    sql.append("SELECT * FROM ").append(PojoTables.POSTMAN_VERITY)
                            .append(" WHERE username ='").append(user.getUsername()).append("' ")
                            .append(" AND verifyStatus = ").append(POSTMAN_VERIFY_STATUS.BACK)
                            .append(" AND delFlag = 0")
                            .append(" ORDER BY createtime DESC")
                            .append(" LIMIT 0,1");
                    System.out.println(sql.toString());
                    try {
                        infoDB = (CityPostmanVerify) UserDaoSql.getInstance().getObject(CityPostmanVerify.class, sql.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("获取失败，请稍后再试");
                    }
                    if (null != infoDB) {
                        StringBuilder sq = new StringBuilder();
                        sq.append("update ").append(PojoTables.POSTMAN_VERITY)
                                .append(" set isVerify =0 ")
                                .append(" ,verifyStatus=").append(POSTMAN_VERIFY_STATUS.SUBMIT)
                                .append(" , name ='").append(info.getOwner()).append("'")
                                .append(",imageUrl ='").append(info.getImageUrl1()).append("'")
                                .append(",Owner ='").append(info.getOwner()).append("'")
                                .append(",OwnerID ='").append(info.getOwnerID()).append("'")
                                .append(" ,updateUser = '").append(user.getUsername())
                                .append("' where id= ").append(infoDB.getId());
                        System.out.println(sq.toString());
                        try {
                            UserDaoSql.getInstance().executeBySql(sq.toString());
                        } catch (SQLException e) {
                            e.printStackTrace();
                            return AjaxResult.errorResult("新增失败，请稍后再试.");
                        }

                        //
                        CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(username);
                        if (null != cityPostman && cityPostman.getIsVerify() != 1) {
                            StringBuilder sb4 = new StringBuilder();
                            sb4.append("update ").append(PojoTables.CityPostman).append(" set isVerify = 0")
                                    .append(",updateUser = '").append(user.getUsername())
                                    .append("' where id = ").append(cityPostman.getId()).append("");
                            try {
                                UserDaoSql.getInstance().executeBySql(sb4.toString());
                            } catch (SQLException e) {
                                e.printStackTrace();
                                return AjaxResult.errorResult("审核失败，请稍后再试");
                            }
                            CityPostmanMemory.getInstance().modifyByUsername(cityPostman.getUsername());
                            try {
                                JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.POSTMAN_INFO_MODIFY).setStr01(cityPostman.getUsername());
                                JmsSender.setTopicSender(notify);
                            } catch (Exception e) {
                            }
                        }
                    } else {
                        info.setCreateUser(user.getUsername());
                        long time = DateUtils.getCurrTime();
                        info.setCreateTime(time);
                        info.setDelFlag(0);
                        info.setIsVerify(0);
                        info.setUsername(username);

                        if (ValidateUtil.isNull(info.getName())) {
                            info.setName(info.getOwner());
                        }
                        if (ValidateUtil.isNull(info.getTelephone())) {
                            info.setTelephone(user.getUsername());
                        }
                        if (ValidateUtil.isNull(info.getImageUrl())) {
                            info.setImageUrl(user.getSignPhoto());
                        }

                        try {
                            info.setVerifyStatus(POSTMAN_VERIFY_STATUS.SUBMIT);
                            UserDaoSql.getInstance().insertObj(PojoTables.POSTMAN_VERITY, info);
                        } catch (SQLException e) {
                            e.printStackTrace();
                            return AjaxResult.errorResult("新增失败，请稍后再试.");
                        }
                    }
                }
            }
            break;
            case 3: {//资料
                if (ValidateUtil.isNotID(info.getId())) {
                    return AjaxResult.errorResult("id不能为空");
                }
                //紧急联系人姓名、紧急联系人电话
                if (ValidateUtil.isNull(info.getSosUser()) || ValidateUtil.isNull(info.getSosPhone())) {
                    return AjaxResult.errorResult("请输入紧急联系人信息");
                }

                //工作地点
                if (ValidateUtil.isNull(info.getProvince()) || ValidateUtil.isNull(info.getCity())) {
                    return AjaxResult.errorResult("请先选择城市");
                }
                if (SYS_SETTINGS.THIRD_PARTY_COMPANY != null && SYS_SETTINGS.THIRD_PARTY_COMPANY == 1) {
                    // List<PostCompanyInfo> companyInfos = PostCompanyMemory.getInstance().listOfCity(info.getCity(), POST_COMPANY_TYPE.PLATE);
                    // if (companyInfos == null || companyInfos.size() == 0) {
                    //     return AjaxResult.errorResult("当前城市尚未开通");
                    // }
                }
                QueryCondition query = QueryCondition.getInstance();
                Expression expression = Expression.getInstance();
                expression.addChild("delFlag", 0);      //所有未删除的
                expression.addChild("username", username);
                expression.addChild("verifyStatus", POSTMAN_VERIFY_STATUS.BACK);
                if (SYS_SETTINGS.THIRD_PARTY_COMPANY != null && SYS_SETTINGS.THIRD_PARTY_COMPANY == 1) {
                    if (info.getType() == null) {
                        return AjaxResult.errorResult("类型不能为空");
                    }
                    expression.addChild("type", info.getType());
                }
                query.addCondition(expression);
                int count = 0;
                try {
                    count = UserDaoSql.getInstance().getCount(PojoTables.POSTMAN_VERITY, query);
                } catch (SQLException e) {
                    e.printStackTrace();
                    return AjaxResult.errorResult("查询失败，请稍后再试");
                }
                String extra1 = "";
                if (ValidateUtil.isNotNull(info.getExtra1())) {
                    extra1 = ",extra1='" + info.getExtra1() + "'";
                }
                String extra2 = "";
                if (ValidateUtil.isNotNull(info.getExtra2())) {
                    extra2 = ",extra2='" + info.getExtra2() + "'";
                }
                String extra3 = "";
                if (ValidateUtil.isNotNull(info.getExtra3())) {
                    extra3 = ",extra3='" + info.getExtra3() + "'";
                }
                String businessUsername = "";
                if (ValidateUtil.isNotNull(info.getBusinessUsername())) {
                    businessUsername = ",businessUsername='" + info.getBusinessUsername() + "'";
                }
                String image3 = "";//手持身份证
                if (ValidateUtil.isNotNull(info.getImageUrl3())) {
                    image3 = ",imageUrl3='" + info.getImageUrl3() + "'";
                }
                String image4 = "";//驾驶证
                if (ValidateUtil.isNotNull(info.getImageUrl4())) {
                    image4 = ",imageUrl4='" + info.getImageUrl4() + "'";
                }
                String image5 = "";//行驶证
                if (ValidateUtil.isNotNull(info.getImageUrl5())) {
                    image5 = ",imageUrl5='" + info.getImageUrl5() + "'";
                }
                String image6 = "";//车辆照片
                if (ValidateUtil.isNotNull(info.getImageUrl6())) {
                    image6 = ",imageUrl6='" + info.getImageUrl6() + "'";
                }
                String image7 = "";//手持驾驶证
                if (ValidateUtil.isNotNull(info.getImageUrl7())) {
                    image7 = ",imageUrl7='" + info.getImageUrl7() + "'";
                }
                String image8 = "";//健康证
                if (ValidateUtil.isNotNull(info.getImageUrl8())) {
                    image8 = ",imageUrl8='" + info.getImageUrl8() + "'";
                }
                String image9 = "";// 健康证反面照
                if (ValidateUtil.isNotNull(info.getImageUrl9())) {
                    image9 = ",imageUrl9='" + info.getImageUrl9() + "'";
                }
                String image10 = "";// 健康证正面照
                if (ValidateUtil.isNotNull(info.getImageUrl10())) {
                    image10 = ",imageUrl10='" + info.getImageUrl10() + "'";
                }
                String image11 = "";// 骑手正面照
                if (ValidateUtil.isNotNull(info.getImageUrl11())) {
                    image11 = ",imageUrl11='" + info.getImageUrl11() + "'";
                }
                String healthNo = "";// 健康证序号
                if (ValidateUtil.isNotNull(info.getHealthNo())) {
                    healthNo = ",healthNo='" + info.getHealthNo() + "'";
                }
                //车辆颜色
                String carColor = "";
                if (ValidateUtil.isNotNull(info.getCarColor())) {
                    carColor = ",carColor='" + info.getCarColor() + "'";
                }
                //车牌号
                String carNo = "";
                if (ValidateUtil.isNotNull(info.getCarNo())) {
                    carNo = ",carNo='" + info.getCarNo() + "'";
                }
                //车辆类型
                String carType = "";
                if (ValidateUtil.isNotNull(info.getCarType())) {
                    carType = ",carType='" + info.getCarType() + "'";
                }
                //车辆类型
                String tipPaotuiIdStr = "";
                if (ValidateUtil.isID(info.getCarTipId())) {
                    tipPaotuiIdStr = ",carTipId=" + info.getCarTipId();
                }

                //商圈id
                String agentId = "";
                if (ValidateUtil.isAbsLong(info.getAgentId())) {
                    agentId = ",agentId='" + info.getAgentId() + "'";
                }
                //配送员类型
                String type = "";
                if (null != info.getType()) {
                    type = ",type=" + info.getType();
                }

                StringBuffer sb = new StringBuffer();
                sb.append("update ").append(PojoTables.POSTMAN_VERITY).append(" set province='").append(info.getProvince())
                        .append("',city='").append(info.getCity())
                        .append("',county='").append(info.getCounty())
                        .append("',verifyStatus=").append(POSTMAN_VERIFY_STATUS.ING)
                        .append(" ,isVerify= 0")
                        .append(",sosUser='").append(info.getSosUser()).append("',sosPhone='").append(info.getSosPhone())
                        .append("',province='").append(info.getProvince()).append("',city='").append(info.getCity()).append("',county='").append(info.getCounty()).append("'")
                        .append(extra1).append(extra2).append(extra3)
                        .append(businessUsername).append(agentId).append(image3)
                        .append(image4).append(image5).append(image6).append(image7).append(image8)
                        .append(image9).append(image10).append(image11).append(healthNo)
                        .append(carColor).append(carNo).append(carType)
                        .append(tipPaotuiIdStr)
                        .append(type)
                        .append(",updateUser = '").append(user.getUsername())
                        .append("'  where delFlag = 0 and username = '").append(username).append("'");
//                if (count > 0) {    //  退回的
//                    sb.append(" and verifyStatus =").append(POSTMAN_VERIFY_STATUS.BACK);
//                } else {
//                    sb.append(" and verifyStatus =").append(POSTMAN_VERIFY_STATUS.SUBMIT);
//                }
                if (SYS_SETTINGS.THIRD_PARTY_COMPANY == null || SYS_SETTINGS.THIRD_PARTY_COMPANY == 0) {
                    if (count > 0) {    //  退回的
                        sb.append(" and verifyStatus =").append(POSTMAN_VERIFY_STATUS.BACK);
                    } else {
                        sb.append(" and verifyStatus =").append(POSTMAN_VERIFY_STATUS.SUBMIT);
                    }
                } else {
                    if (count > 0) {    //  退回的
                        sb.append(" and verifyStatus =").append(POSTMAN_VERIFY_STATUS.BACK)
                                .append(" and type =").append(info.getType());
                    } else {
                        sb.append(" and verifyStatus =").append(POSTMAN_VERIFY_STATUS.SUBMIT);
                    }
                }
                try {
                    UserDaoSql.getInstance().executeBySql(sb.toString());
                } catch (SQLException e) {
                    e.printStackTrace();
                    return AjaxResult.errorResult("上传信息失败，请稍后再试");
                }

                Long time = DateUtils.getCurrTime();
                // 修改city_postman表
//                CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(info.getUsername());
                CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(username);
                if (null == cityPostman) {
//                    cityPostman = new CityPostman(username, info.getProvince(), info.getCity(), info.getCounty(), "", "", user.getUsername(), time);
                    cityPostman = new CityPostman(username, info.getType() == null ? POSTMAN_TYPE.POSTMAN : info.getType(), info.getProvince(), info.getCity(), info.getCounty(), "", "", user.getUsername(), time);
                    cityPostman.setIsVerify(2);
                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.CityPostman, cityPostman);
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("添加失败，请稍后再试");
                    }
                    //缓存中新增配送员
                    CityPostmanMemory.getInstance().add(time);
                    try {
                        JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.POSTMAN_INFO_ADD).setLong01(time);
                        JmsSender.setTopicSender(notify);
                    } catch (Exception e) {
                    }
                }
                if (null != cityPostman && cityPostman.getIsVerify() == 0) {
                    StringBuilder sb4 = new StringBuilder();
                    sb4.append("update ").append(PojoTables.CityPostman).append(" set isVerify = 2")
                            .append(",updateUser = '").append(user.getUsername())
                            .append("' where username = '").append(user.getUsername()).append("'");
                    try {
                        UserDaoSql.getInstance().executeBySql(sb4.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("审核失败，请稍后再试");
                    }
                    CityPostmanMemory.getInstance().reloadCity(cityPostman.getCity());
                    try {
                        JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.POSTMAN_RELOAD_BY_CITY).setStr01(cityPostman.getCity());
                        JmsSender.setTopicSender(notify);
                    } catch (Exception e) {
                    }
                }

                //修改用户角色
                if (isExactUserType) {
                    // 店铺也能认证

                } else {
                    if (0 == UserType.USER - user.getUserType()) {
                        User.modifyUserType(user.getUsername(), UserType.POSTMAN);
                    }
                }
//                if (0 == UserType.USER - user.getUserType()) {
//                    User.modifyUserType(user.getUsername(), UserType.POSTMAN);
//                }
                //推送通知
                Notice.publishNotice2Person(sysAppKey, user.getUsername(), JPushMessage.POSTMAN, NoticeType.system, "提交成功", "您已成功提交的认证，请耐心等待");
            }
            break;
        }
        return AjaxResult.successResult();
    }

    // todo: 再次申请骑手类型
    public static void applyPostManType(String username, CityPostmanVerify info) throws Exception {
        if (info.getType() == null) {
            throw new Exception("请选择类型");
        }
        CityPostman postman = CityPostmanMemory.getInstance().getPostman(username);
        if (postman == null) {
            throw new Exception("当前未获取到骑手信息");
        }

        Set<Long> postTypeIdSet = PostmanTypeRelationMemory.getInstance().listOfByPostman(username);
        PostTypeInfo postTypeInfo = PostTypeMemory.getInstance().getByCode(info.getType());
        if (postTypeInfo == null) {
            throw new Exception("未获取到配送员类型,请核实");
        }
        if (postTypeIdSet != null && !postTypeIdSet.isEmpty() && postTypeIdSet.contains(postTypeInfo.getId())) {
            throw new Exception("你已经申请过了,无需重复申请");
        }
        QueryCondition query = QueryCondition.getInstance();
        Expression expression = Expression.getInstance();
        expression.addChild("delFlag", 0);//所有未删除的
        expression.addChild("username", username);
        query.addCondition(expression);
        List<Order> orders = new ArrayList<>();
        orders.add(new Order("id", Order.DESC));
        query.setOrders(orders);
        String sql = DaoUtils.getSql(PojoTables.POSTMAN_VERITY, query);
        List<CityPostmanVerify> cityPostmanVerifyList = null;
        try {
            cityPostmanVerifyList = (List<CityPostmanVerify>) UserDaoSql.getInstance().list(CityPostmanVerify.class, sql.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        }
        boolean hasBack = false;
        CityPostmanVerify cityPostmanVerify_db = null;
        CityPostmanVerify cityPostmanVerify_Over = null;
        if (ValidateUtil.isAbsList(cityPostmanVerifyList)) {
            for (CityPostmanVerify verify : cityPostmanVerifyList) {
                if (verify == null) {
                    continue;
                }
                if (verify.getVerifyStatus() - POSTMAN_VERIFY_STATUS.OVER == 0) {
                    cityPostmanVerify_Over = verify;
                }
                if (cityPostmanVerify_Over == null && verify.getVerifyStatus() - POSTMAN_VERIFY_STATUS.ING == 0) {
                    cityPostmanVerify_Over = verify;
                }
                if (cityPostmanVerify_Over == null && verify.getVerifyStatus() - POSTMAN_VERIFY_STATUS.BACK == 0) {
                    cityPostmanVerify_Over = verify;
                }
                if (verify.getType() != null && verify.getType() - info.getType() == 0) {
                    if (verify.getVerifyStatus() - POSTMAN_VERIFY_STATUS.BACK == 0) {
                        hasBack = true;
                    }
                    if (verify.getVerifyStatus() - POSTMAN_VERIFY_STATUS.ING == 0) {
                        cityPostmanVerify_db = verify;
                        break;
                    }
                }
            }
        }

        if (cityPostmanVerify_db != null) {
            throw new Exception("您已经申请过了,无需重复申请");
        }
        if (hasBack) {
            // 删除已经退回的
            StringBuffer updateSql = new StringBuffer();
            updateSql.append("update ").append(PojoTables.POSTMAN_VERITY)
                    .append(" set delFlag  = 1 and updateUser ='").append(username).append("'")
                    .append(" where delFlag = 0 and type =").append(info.getType())
                    .append(" and username = '").append(username).append("'");
            try {
                UserDaoSql.getInstance().executeBySql(updateSql.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (cityPostmanVerify_Over != null) {
            ObjectUtils.copyPropertiesExclude(info, cityPostmanVerify_Over);
            cityPostmanVerify_Over.setId(null);
            cityPostmanVerify_Over.setCreateTime(DateUtils.getCurrTime());
            cityPostmanVerify_Over.setCreateUser(username);
            cityPostmanVerify_Over.setUpdateUser(null);
            cityPostmanVerify_Over.setType(info.getType());
            cityPostmanVerify_Over.setCarTipId(info.getCarTipId());
            cityPostmanVerify_Over.setIsVerify(0);
            cityPostmanVerify_Over.setVerifyStatus(POSTMAN_VERIFY_STATUS.ING);
            UserDaoSql.getInstance().saveObj(PojoTables.POSTMAN_VERITY, cityPostmanVerify_Over);
        } else {
            info.setVerifyStatus(POSTMAN_VERIFY_STATUS.ING);
            info.setCreateTime(DateUtils.getCurrTime());
            info.setDelFlag(0);
            // info.setCity(postman.getCity());
            // info.setCounty(postman.getCounty());
            info.setIsVerify(0);
            // info.setProvince(postman.getProvince());
            UserInfo user = UserInfoMemory.getInstance().getUserInfo(username);
            if (user != null) {
                info.setName(user.getName());
            }
            info.setSex(0);
            if (ValidateUtil.isNotID(info.getAgentId())) {
                List<Long> list = PostmanAgentRelationMemory.getInstance().listOfAgentIdByPostman(username);
                if (ValidateUtil.isAbsList(list)) {
                    info.setAgentId(list
                            .get(0));
                }
            }
            UserDaoSql.getInstance().saveObj(PojoTables.POSTMAN_VERITY, info);
        }
    }


    //------------------------------------管理员
    //获取待审核的配送员列表  管理员
    @RequestMapping(method = RequestMethod.POST, value = "/admin/post/verify/list")
    @ResponseBody
    public String list(String token, String city,
                       String province, String county,
                       Integer type, // 配送员类型  POSTMAN_TYPE // 1配送员  2工人  3提货点  4货运司机   6二级骑手
                       String username,
                       String agentUsername, // 代理账号
                       Long agentId,    // 商圈id
                       Long startTime, Long endTime,
                       Integer page, Integer size) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }


        if (ValidateUtil.isNotAbsInteger(page)) {
            page = 1;
        }
        if (ValidateUtil.isNotAbsInteger(size)) {
            size = 10;
        }

//            QueryCondition query = QueryCondition.getInstance();
//            Expression expression = Expression.getInstance();
//            expression.addChild("delFlag", 0);//所有未删除的
//            expression.addChild("isVerify", 0);//未审核
//            if (ValidateUtil.isNotNull(city)) {
//                expression.addChild("city", city);
//            }
//            if (ValidateUtil.isNotNull(province)) {
//            expression.addChild("province", province);
//             }
//        if (ValidateUtil.isNotNull(county)) {
//            expression.addChild("county", county);
//        }
//        if (ValidateUtil.isNotNull(agentUsername)) {
//            expression.addChild("agentUsername", "=", agentUsername);
//        }
//        if (ValidateUtil.isNotNull(username)) {
//            expression.addChild("username", "like", username);
//        }
//        if (null != startTime) {
//            expression.addChild("createTime", ">=", startTime);
//        }
//        if (null != endTime) {
//            expression.addChild("createTime", "<=", endTime);
//        }
////
////        if (ValidateUtil.isAbsInteger(typeId)) {
////            expression.addChild("typeId", typeId);
////        }
////        if (ValidateUtil.isNotNull(content)) {
////            expression.addChild("ownUsername", "like", content);
////        }
//
//        if (0 == UserType.BUSINESS_MAN - user.getUserType()) {
//            expression.addChild("businessUsername", user.getUsername());
//        }
//
//        // 配送员类型
//        if (null != type) {
//            expression.addChild("type", type);
//        }
//
//        expression.addChild("verifyStatus", POSTMAN_VERIFY_STATUS.ING);
//
//        query.addCondition(expression);
//        query.addOrders("createTime", "desc");
//        query.setPage(page);
//        query.setSize(size);


        StringBuffer sb = new StringBuffer();
        StringBuffer sbCount = new StringBuffer();

        sb.append("select pv.* from ").append(PojoTables.POSTMAN_VERITY).append(" pv ");
        if (0 == UserType.ADMIN - user.getUserType() || SysCityCompanyMan.isAdminCompanyUser(user.getUsername())) {
            sb.append(" right join ").append(PojoTables.USER_AGENT_RELATION)
                    .append(" uar on pv.agentId=uar.agentId ");
        }
        sb.append(" where pv.delFlag=0 ");

        if (0 == UserType.ADMIN - user.getUserType() || SysCityCompanyMan.isAdminCompanyUser(user.getUsername())) {
            sb.append(" and uar.delFlag = 0 ");
        }


        sb.append(" AND pv.isVerify=0 AND pv.verifyStatus=").append(POSTMAN_VERIFY_STATUS.ING);


        if (ValidateUtil.isNotNull(province)) {
            sb.append(" and pv.province=").append("'").append(province).append("'");
        }
        if (ValidateUtil.isNotNull(city)) {
            sb.append(" and pv.city=").append("'").append(city).append("'");
        }
        if (ValidateUtil.isNotNull(county)) {
            sb.append(" and pv.county=").append("'").append(county).append("'");
        }
        if (null != agentId) {
            sb.append(" and pv.agentId=").append(agentId);
        }
        if (ValidateUtil.isNotNull(agentUsername) && (0 == UserType.ADMIN - user.getUserType() || SysCityCompanyMan.isAdminCompanyUser(user.getUsername()))) {
            agentUsername = SysCityCompanyMan.getSysCompanyOwnUsername(agentUsername);
            sb.append(" and uar.username='").append(agentUsername).append("' ");
        }
        if (ValidateUtil.isNotNull(username)) {
            sb.append(" and pv.username='").append(username).append("' ");
        }
        if (null != startTime) {
            sb.append(" and pv.createTime >=").append(startTime);
        }
        if (null != endTime) {
            sb.append(" and pv.createTime <=").append(endTime);
        }


        if (0 == UserType.BUSINESS_MAN - user.getUserType()) {
            sb.append(" and pv.businessUsername = '").append(user.getUsername()).append("' ");
        }

        // 配送员类型
        if (null != type) {
            sb.append(" and pv.type=").append(type);
        }
        sbCount.append(sb);

        sb.append(" order by pv.createTime desc limit ").append((page - 1) * size).append(",").append(size);


//
//        String sql = DaoUtils.getSql(PojoTables.POSTMAN_VERITY, query);
//        String sqlCount = DaoUtils.getSqlCount(PojoTables.POSTMAN_VERITY, query);
        List<CityPostmanVerify> list = null;
        List<CityPostmanVerify> lists = null;
//        Long totalCount = 0l;
//        try {
//            list = (List<CityPostmanVerify>) UserDaoSql.getInstance().list(CityPostmanVerify.class, sql);
//            totalCount = UserDaoSql.getInstance().getCount(sqlCount);
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return AjaxResult.errorResult("获取失败，请稍后再试");
//        }
        try {
            list = (List<CityPostmanVerify>) UserDaoSql.getInstance().list(CityPostmanVerify.class, sb.toString());
            lists = (List<CityPostmanVerify>) UserDaoSql.getInstance().list(CityPostmanVerify.class, sbCount.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("获取失败，请稍后再试");
        }
        Integer totalCounts = 0;
        if (null != lists && lists.size() > 0) {
            totalCounts = lists.size();
        }


//        if (ValidateUtil.isNotAbsInteger(page)) {
//            page = 1;
//        }
//        if (ValidateUtil.isNotAbsInteger(size)) {
//            size = 20;
//        }
//        JYPageUtils.PageList pageList = JYPageUtils.getList(list, page, size);
//        if (null != pageList && null != pageList.list && pageList.list.size() > 0) {
//            List<CityPostmanVerify> voList = new ArrayList<CityPostmanVerify>();
//            for (int i = 0; i < pageList.list.size(); i++) {
//                if (null == pageList.list.get(i)) {
//                    continue;
//                }
//                CityPostmanVerify info = new CityPostmanVerify();
//                try {
//                    ObjectUtils.copyPropertiesExclude(pageList.list.get(i), info, null);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    return AjaxResult.errorResult("获取失败，请稍后再试");
//                }
//                if (null == info) {
//                    continue;
//                }
//                if (ValidateUtil.isNotNull(info.getImageUrl()) && ValidateUtil.isNotNetUrl(info.getImageUrl())) {
//                    info.setImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl());
//                }
//                if (ValidateUtil.isNotNull(info.getImageUrl1()) && ValidateUtil.isNotNetUrl(info.getImageUrl1())) {
//                    info.setImageUrl1(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl1());
//                }
//                if (ValidateUtil.isNotNull(info.getImageUrl2()) && ValidateUtil.isNotNetUrl(info.getImageUrl2())) {
//                    info.setImageUrl2(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl2());
//                }
//                if (ValidateUtil.isNotNull(info.getImageUrl3()) && ValidateUtil.isNotNetUrl(info.getImageUrl3())) {
//                    info.setImageUrl3(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl3());
//                }
//                if (ValidateUtil.isNotNull(info.getImageUrl4()) && ValidateUtil.isNotNetUrl(info.getImageUrl4())) {
//                    info.setImageUrl4(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl4());
//                }
//
//                voList.add(info);
//            }
//            if (voList.size() > 0) {
//                return AjaxResult.successResult(pageList.totalCount, voList.size(), voList);
//            }
//        }
        if (null == totalCounts) {
            totalCounts = 0;
        }

        List<CityPostmanVerifyVO> voList = new ArrayList<>();
        if (ValidateUtil.isAbsList(list)) {
            for (int i = 0; i < list.size(); i++) {
                if (null == list.get(i)) {
                    continue;
                }
                CityPostmanVerifyVO vo = new CityPostmanVerifyVO();

                if (ValidateUtil.isNotNull(list.get(i).getImageUrl()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl())) {
                    list.get(i).setImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl());
                }
                if (ValidateUtil.isNotNull(list.get(i).getImageUrl1()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl1())) {
                    list.get(i).setImageUrl1(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl1());
                }
                if (ValidateUtil.isNotNull(list.get(i).getImageUrl2()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl2())) {
                    list.get(i).setImageUrl2(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl2());
                }
                if (ValidateUtil.isNotNull(list.get(i).getImageUrl3()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl3())) {
                    list.get(i).setImageUrl3(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl3());
                }
                if (ValidateUtil.isNotNull(list.get(i).getImageUrl4()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl4())) {
                    list.get(i).setImageUrl4(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl4());
                }
                if (ValidateUtil.isNotNull(list.get(i).getImageUrl5()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl5())) {
                    list.get(i).setImageUrl5(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl5());
                }
                if (ValidateUtil.isNotNull(list.get(i).getImageUrl6()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl6())) {
                    list.get(i).setImageUrl6(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl6());
                }
                if (ValidateUtil.isNotNull(list.get(i).getImageUrl7()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl7())) {
                    list.get(i).setImageUrl7(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl7());
                }
                if (ValidateUtil.isNotNull(list.get(i).getImageUrl8()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl8())) {
                    list.get(i).setImageUrl8(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl8());
                }
                BeanUtils.copyProperties(list.get(i), vo);
                if (ValidateUtil.isAbsInteger(vo.getType())) {
                    PostTypeInfo typeInfo = PostTypeMemory.getInstance().getByCode(vo.getType());
                    if (typeInfo != null) {
                        vo.setPostTypeName(typeInfo.getName());
                    }
                }
                voList.add(vo);
            }
            // 处理押金问题
            voList = voList.stream().map(a -> dealPostManDepositMoney(a)).collect(Collectors.toList());
//            return AjaxResult.successResult(totalCounts, list.size(), list);
            return AjaxResult.successResult(totalCounts, voList.size(), voList);
        }
        return AjaxResult.successResult(0, 0, null);
    }

    //审核配送员  管理员
    @RequestMapping(method = RequestMethod.POST, value = "/admin/post/verify")
    @ResponseBody
    public String post_verify(String token, String sysAppKey, Long id, Long agentId) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }

        if (ValidateUtil.isNotID(id)) {
            return AjaxResult.errorResult("被审核人ID不能为空");
        }
        //  1.根据id 查询出该配送员得信息
        QueryCondition query = QueryCondition.getInstance();
        Expression expression = Expression.getInstance();
        expression.addChild("delFlag", 0);//所有未删除的
//        expression.addChild("isVerify", 0);//未审核
        expression.addChild("id", id);
        query.addCondition(expression);
//        query.addOrders("createTime", "desc");
        String sql = DaoUtils.getSql(PojoTables.POSTMAN_VERITY, query);

        CityPostmanVerify cityPostmanVerify = null;
        try {
            cityPostmanVerify = (CityPostmanVerify) UserDaoSql.getInstance().getObject(CityPostmanVerify.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("审核失败，请稍后再试");
        }
        //todo 1. 是否有这个
        if (null == cityPostmanVerify) {
            return AjaxResult.errorResult("未查询出此人信息！");
        }

        if (0 == cityPostmanVerify.getVerifyStatus() - POSTMAN_VERIFY_STATUS.OVER) {
            return AjaxResult.errorResult("审核已经通过，无需再审核");
        }
        if (0 == cityPostmanVerify.getVerifyStatus() - POSTMAN_VERIFY_STATUS.SUBMIT) {
            return AjaxResult.errorResult("尚未提交审核，无法操作");
        }


        //todo 2. 之前是不是已经有了这个配送员（缓存里去查）
        //  2.将信息添加到到配送员表中
        CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(cityPostmanVerify.getUsername());
        if (null == cityPostman) {
            long time = DateUtils.getCurrTime();
//        StringBuilder sb1 = new StringBuilder();
//        sb1.append("INSERT INTO ").append(PojoTables.CityPostman).append("(username, isVerify, province, city,county, verifyTime, createTime)")
//                .append(" VALUES ('")
//                .append(cityPostmanVerify.getUsername()).append("', 1 ,'")
//                .append(cityPostmanVerify.getProvince()).append("','")
//                .append(cityPostmanVerify.getCity()).append("','")
//                .append(cityPostmanVerify.getCounty()).append("',")
//                .append(time).append(",").append(time).append(")");
//        try {
//            UserDaoSql.getInstance().executeBySql(sb1.toString());
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return AjaxResult.errorResult("审核失败，请稍后再试");
//        }
//            cityPostman = new CityPostman(cityPostmanVerify.getUsername(), cityPostmanVerify.getProvince(), cityPostmanVerify.getCity(), cityPostmanVerify.getCounty(), "", "", user.getUsername(), time);
            cityPostman = new CityPostman(cityPostmanVerify.getUsername(), cityPostmanVerify.getType() == null ? POSTMAN_TYPE.POSTMAN : cityPostmanVerify.getType(), cityPostmanVerify.getProvince(), cityPostmanVerify.getCity(), cityPostmanVerify.getCounty(), "", "", user.getUsername(), time);
            cityPostman.setIsVerify(1);
            cityPostman.setApplyTime(cityPostmanVerify.getCreateTime());
            if (null != cityPostmanVerify.getType()) {
                cityPostman.setType(cityPostmanVerify.getType());
            }
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.CityPostman, cityPostman);
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("添加失败，请稍后再试");
            }
            //缓存中新增配送员
            CityPostmanMemory.getInstance().add(time);
            try {
                JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.POSTMAN_INFO_ADD).setLong01(time);
                JmsSender.setTopicSender(notify);
            } catch (Exception e) {
            }
            cityPostman = CityPostmanMemory.getInstance().getPostman(cityPostmanVerify.getUsername());
        }
        if (null != cityPostman && cityPostman.getIsVerify() != 1) {
//            StringBuilder sb2 = new StringBuilder();
//            sb2.append("update ").append(PojoTables.CityPostman).append(" set userType =").append(UserType.POSTMAN)
//                    .append(" where username = '").append(cityPostmanVerify.getUsername()).append("' and userType = ").append(UserType.USER);
//            try {
//                UserDaoSql.getInstance().executeBySql(sb2.toString());
//            } catch (SQLException e) {
//                e.printStackTrace();
//                return AjaxResult.errorResult("审核失败，请稍后再试");
//            }
//            //修改配送员的用户类型 缓存
//            UserInfoMemory.getInstance().modifyUserType(cityPostmanVerify.getUsername(), UserType.USER);
            StringBuilder sb4 = new StringBuilder();
            sb4.append("update ").append(PojoTables.CityPostman).append(" set isVerify = 1")
                    .append(",updateUser = '").append(user.getUsername())
                    .append("' where username = '").append(cityPostmanVerify.getUsername()).append("'");
            try {
                UserDaoSql.getInstance().executeBySql(sb4.toString());
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("审核失败，请稍后再试");
            }

//            cityPostman.setIsVerify(1);      //   已经有配送员但是没有认证
            CityPostmanMemory.getInstance().reloadCity(cityPostman.getCity());
            try {
                JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.POSTMAN_RELOAD_BY_CITY).setStr01(cityPostman.getCity());
                JmsSender.setTopicSender(notify);
            } catch (Exception e) {
            }
        }

        boolean isExactUserType = User.isExactUserType(); // 是否多权限的

        //todo 3. 该账号是不是已经是配送员类型了 如果是的话就不更改了

        //检测用户是否已经存在
        UserInfo userInfo = UserInfoMemory.getInstance().getUserInfo(cityPostmanVerify.getUsername());
        if (null == userInfo) {//系统中无该用户
            //新建管理员
            UserInfo u = new UserInfo(UserType.POSTMAN, cityPostmanVerify.getUsername(), cityPostmanVerify.getName(), "", cityPostmanVerify.getTelephone(), "", "",
                    cityPostmanVerify.getProvince(), cityPostmanVerify.getCity(), cityPostmanVerify.getCounty(), "", user.getUsername());
            try {
                String psw = "123456";
                if (null != SYS_SETTINGS.SHOP_DEFAULT_PSW && 0 == 1 - SYS_SETTINGS.SHOP_DEFAULT_PSW) {
                    psw = cityPostmanVerify.getUsername().substring(5, 11);
                }
                ResultInfo resultInfo = User.addUser(u, psw);
                if (!resultInfo.isSuccess()) {
                    return AjaxResult.errorResult(resultInfo.getError());
                }
            } catch (Exception e) {
                return AjaxResult.errorResult("新增失败，请稍后再试");
            }

        } else {//修改为配送员
            if (isExactUserType) {
                // 多权限的话  不修改权限为骑手的

            } else {
                if (UserType.POSTMAN != userInfo.getUserType()) {
                    User.modifyUserType(cityPostmanVerify.getUsername(), UserType.POSTMAN);
                }
            }

        }


//       CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(cityPostmanVerify.getUsername());
//        cityPostman.setUsername(cityPostmanVerify.getUsername());
//        cityPostman.setIsVerify(1);
//        cityPostman.setProvince(cityPostmanVerify.getProvince());
//        cityPostman.setCity(cityPostmanVerify.getCity());
//        cityPostman.setCounty(cityPostmanVerify.getCounty());
//        cityPostman.setVerifyTime(time);
//        cityPostman.setCreateTime(time);
//        cityPostman.setCreateUser(user.getUsername());


//        if (ValidateUtil.isAbsList(list)) {
//            for (int i = 0; i < list.size(); i++) {
//                CityPostman cityPostman = new CityPostman();
//                try {
//                    ObjectUtils.copyPropertiesExclude(list.get(i), cityPostman, null);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    continue;
//                }
//
//                if (null == cityPostman) {
//                    continue;
//                }
////                shopInfo.setImageUrl(list.get(i).getShopName());
////                shopInfo.setId(null);
////                ResultInfo resultInfo = Shop.addShop(user.getUsername(), cityPostman, list.get(i).getShopTypes(), null);
////                if (resultInfo.isSuccess()) {
////                    continue;
////                } else {
////                    return AjaxResult.errorResult(resultInfo.getError());
////                }
//            }
//        }

        // 将审核过的店铺标记为已审核

        // 3 将用户表的UserType 由普通改为配送员
//        StringBuilder sb2 = new StringBuilder();
//        sb2.append("update ").append(PojoTables.UserInfo).append(" set isVerify =1, ,name = '").append(cityPostmanVerify.getName())
//                .append("' where username = '").append(cityPostmanVerify.getUsername()).append("' and delFlag = 0 ");
//        try {
//            UserDaoSql.getInstance().executeBySql(sb2.toString());
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return AjaxResult.errorResult("审核失败，请稍后再试");
//        }
//        //修改配送员的用户类型 缓存
//        UserInfoMemory.getInstance().modifyUserType(cityPostmanVerify.getUsername(), UserType.USER);

        StringBuilder sb5 = new StringBuilder();
        sb5.append("update ").append(PojoTables.UserInfo).append(" set name = '").append(cityPostmanVerify.getName())
                .append("',updateUser='").append(user.getUsername()).append("' ");
        if (ValidateUtil.isNotNull(cityPostmanVerify.getImageUrl())) {
            sb5.append(" ,signPhoto='").append(cityPostmanVerify.getImageUrl()).append("' ");
        }
        sb5.append(" where username = '").append(cityPostmanVerify.getUsername()).append("' and delFlag = 0 ");
        try {
            UserDaoSql.getInstance().executeBySql(sb5.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("审核失败，请稍后再试");
        }
        //修改配送员的用户类型 缓存
        UserInfoMemory.getInstance().modifyUserInfo(cityPostmanVerify.getUsername());
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_INFO_MODIFY).setStr01(cityPostmanVerify.getUsername());//o2o.business.jms  MQDataBusiness
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }

        StringBuilder sb3 = new StringBuilder();
        sb3.append("update ").append(PojoTables.POSTMAN_VERITY).append(" set isVerify =1,updateUser='")
                .append(user.getUsername()).append("',verifyStatus=").append(ShopVerify.OVER)
                .append(" where delFlag = 0 and id =").append(id);
        try {
            UserDaoSql.getInstance().executeBySql(sb3.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("审核失败，请稍后再试");
        }
        // 原先申请的id
        if (!ValidateUtil.isID(agentId)) {
            agentId = cityPostmanVerify.getAgentId();
        }
        //处理商圈
        if (ValidateUtil.isAbsLong(agentId)) {
            CityAgent cAgent = CityAgentMemory.getInstance().getById(agentId);
            if (null == cAgent) {
                return AjaxResult.errorResult("您不是代理，无权新增");
            }

            //添加代理对应的配送员关系
            PostmanAgentRelation relation = new PostmanAgentRelation();
            relation.setAgentId(agentId);
            relation.setAgentUsername(cAgent.getUsername());
            relation.setPostmanUsername(cityPostmanVerify.getUsername());
            if (null != cityPostman) {
                relation.setPostmanId(cityPostman.getId());
            }
            relation.setCreateTime(DateUtils.getCurrTime());
            relation.setDelFlag(0);
            relation.setProvince(cityPostmanVerify.getProvince());
            relation.setCity(cityPostmanVerify.getCity());
            relation.setCounty(cityPostmanVerify.getCounty());

            try {
                UserDaoSql.getInstance().insertObj(PojoTables.POSTMAN_AGENT_RELATION, relation);
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("条件代理关系时失败，请稍后再试");
            }
            PostmanAgentRelationMemory.getInstance().add(agentId);
            // 数据同步
            try {
                JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.AGENT_POSTMAN_RELATION_ADD).setLong01(agentId);
                JmsSender.setTopicSender(notify);
            } catch (Exception e) {
            }
        }
        // 添加配送员与配送类型之间的关系
        if (SYS_SETTINGS.THIRD_PARTY_COMPANY != null && SYS_SETTINGS.THIRD_PARTY_COMPANY == 1) {
            // 配送员与类型之间的关系
            PostmanMan.addPostmanTypeRelation(cityPostmanVerify.getUsername(), cityPostmanVerify.getType(), cityPostmanVerify.getCarTipId(), user.getUsername());
            // 配送员与默认公司的管理
            // PostmanMan.addPostmanPlateCompany(cityPostmanVerify.getUsername(), cityPostmanVerify.getCity(), cityPostmanVerify.getType(), user.getUsername());
        }

        // 推送通知
        Notice.publishNotice2Person(sysAppKey, cityPostmanVerify.getUsername(), JPushMessage.POSTMAN, NoticeType.system, "认证通过", "您提交的认证资料已审核通过，请及时查看");

        JYSmsSeverBusiness.sendSMS(sysAppKey, cityPostmanVerify.getUsername(), SMS_TYPE.POSTMAN_VERIFY_SUCCESS);


        return AjaxResult.successResult();
    }


    // 审核退回  管理员
    @RequestMapping(method = RequestMethod.POST, value = "/admin/postman/verify/back")
    @ResponseBody
    public String post_verify_back(String token, String sysAppKey, CityPostmanVerifyLog info) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (ValidateUtil.isNotID(info.getpId())) {
            return AjaxResult.errorResult("ID不能为空");
        }

        //  1.根据id 查询出该配送员得信息
        QueryCondition query = QueryCondition.getInstance();
        Expression expression = Expression.getInstance();
        expression.addChild("delFlag", 0);//所有未删除的
//        expression.addChild("isVerify", 0);//未审核
        expression.addChild("id", info.getpId());
        query.addCondition(expression);
//        query.addOrders("createTime", "desc");
        String sql = DaoUtils.getSql(PojoTables.POSTMAN_VERITY, query);

        CityPostmanVerify cityPostmanVerify = null;
        try {
            cityPostmanVerify = (CityPostmanVerify) UserDaoSql.getInstance().getObject(CityPostmanVerify.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("审核失败，请稍后再试");
        }
        //todo 1. 是否有这个
        if (null == cityPostmanVerify) {
            return AjaxResult.errorResult("未查询出此人信息！");
        }


        if (POSTMAN_VERIFY_STATUS.ING - cityPostmanVerify.getVerifyStatus() == 0) {
            long time = DateUtils.getCurrTime();
            info.setCreateTime(time);
            info.setStatus(0);
            info.setCreateUser(user.getUsername());
            info.setDelFlag(0);
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.POSTMAN_VERITY_LOG, info);
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("添加失败，请稍后再试");
            }

            StringBuilder sql2 = new StringBuilder();
            sql2.append("update ").append(PojoTables.POSTMAN_VERITY).append(" set verifyStatus = ").append(POSTMAN_VERIFY_STATUS.BACK)
                    .append(", isVerify = 2")
                    .append(",updateUser = '").append(user.getUsername())
                    .append("' where id = ").append(info.getpId());
            try {
                UserDaoSql.getInstance().executeBySql(sql2.toString());
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("审核失败，请稍后再试");
            }
        } else {
            return AjaxResult.errorResult("审核状态不对，请稍后再试");
        }

        //推送通知
        Notice.publishNotice2Person(sysAppKey, cityPostmanVerify.getUsername(), JPushMessage.POSTMAN, NoticeType.system, "认证失败", "您提交的认证资料有误，请重新提交");

        //发送短信
//        SMSInfo smsInfo = new SMSInfo(cityPostmanVerify.getUsername(), null,
//                null, DUBBO_SMS_TYPE.OTHER,
//                SYS_CONFIG_SMS.POSTMAN_VERIFY_FAIL);
//        SMSThreadMap.smsCodeSet.add(smsInfo);

        JYSmsSeverBusiness.sendSMS(sysAppKey, cityPostmanVerify.getUsername(), SMS_TYPE.POSTMAN_VERIFY_FAIL);

        return AjaxResult.successResult();
    }

    // 审核退回列表 管理员
    @RequestMapping(method = RequestMethod.POST, value = "/admin/postman/verify/back/list")
    @ResponseBody
    public String verify_back_list(String token, String city, String username, Long startTime, Long endTime, Long agentId, String agentUsername,
                                   String province, String county, Integer page, Integer size) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }

        if (ValidateUtil.isNotAbsInteger(page)) {
            page = 1;
        }
        if (ValidateUtil.isNotAbsInteger(size)) {
            size = 10;
        }

        StringBuffer sb = new StringBuffer();
        StringBuffer sbCount = new StringBuffer();

        sb.append("select pv.* from ").append(PojoTables.POSTMAN_VERITY).append(" pv ");
        if (0 == UserType.ADMIN - user.getUserType() || SysCityCompanyMan.isAdminCompanyUser(user.getUsername())) {
            sb.append(" right join ").append(PojoTables.USER_AGENT_RELATION)
                    .append(" uar on pv.agentId=uar.agentId ");
        }
        sb.append(" where pv.delFlag=0 ");

        if (0 == UserType.ADMIN - user.getUserType()) {
            sb.append(" and uar.delFlag = 0 ");
        }


        sb.append(" AND pv.isVerify=2 AND pv.verifyStatus=").append(POSTMAN_VERIFY_STATUS.BACK);


        if (ValidateUtil.isNotNull(province)) {
            sb.append(" and pv.province='").append(province).append("'");
        }
        if (ValidateUtil.isNotNull(city)) {
            sb.append(" and pv.city='").append(city).append("'");
        }
        if (ValidateUtil.isNotNull(county)) {
            sb.append(" and pv.county='").append(county).append("'");
        }
        if (null != agentId) {
            sb.append(" and pv.agentId=").append(agentId);
        }
        if (ValidateUtil.isNotNull(agentUsername) && (0 == UserType.ADMIN - user.getUserType() || SysCityCompanyMan.isAdminCompanyUser(agentUsername))) {
            sb.append(" and uar.username='").append(agentUsername).append("'");
        }
        if (ValidateUtil.isNotNull(username)) {
            sb.append(" and pv.username='").append(username).append("'");
        }
        if (null != startTime) {
            sb.append(" and pv.createTime >=").append(startTime);
        }
        if (null != endTime) {
            sb.append(" and pv.createTime <=").append(endTime);
        }


        if (0 == UserType.BUSINESS_MAN - user.getUserType()) {
            sb.append(" and pv.businessUsername = '").append(user.getUsername()).append("'");
        }

        sbCount.append(sb);

        sb.append(" order by pv.createTime desc limit ").append((page - 1) * size).append(",").append(size);


//
//        String sql = DaoUtils.getSql(PojoTables.POSTMAN_VERITY, query);
//        String sqlCount = DaoUtils.getSqlCount(PojoTables.POSTMAN_VERITY, query);
        List<CityPostmanVerifyVO> list = null;
        List<CityPostmanVerifyVO> lists = null;
//        Long totalCount = 0l;
//        try {
//            list = (List<CityPostmanVerify>) UserDaoSql.getInstance().list(CityPostmanVerify.class, sql);
//            totalCount = UserDaoSql.getInstance().getCount(sqlCount);
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return AjaxResult.errorResult("获取失败，请稍后再试");
//        }
        try {
            list = (List<CityPostmanVerifyVO>) UserDaoSql.getInstance().list(CityPostmanVerifyVO.class, sb.toString());
            lists = (List<CityPostmanVerifyVO>) UserDaoSql.getInstance().list(CityPostmanVerifyVO.class, sbCount.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("获取失败，请稍后再试");
        }
        Integer totalCounts = 0;
        if (null != lists && lists.size() > 0) {
            totalCounts = lists.size();
        }
//
        if (ValidateUtil.isAbsList(list)) {
            List<CityPostmanVerifyVO> voList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                if (null == list.get(i)) {
                    continue;
                }
                if (ValidateUtil.isNotNull(list.get(i).getImageUrl()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl())) {
                    list.get(i).setImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl());
                }
                if (ValidateUtil.isNotNull(list.get(i).getImageUrl1()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl1())) {
                    list.get(i).setImageUrl1(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl1());
                }
                if (ValidateUtil.isNotNull(list.get(i).getImageUrl2()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl2())) {
                    list.get(i).setImageUrl2(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl2());
                }
                if (ValidateUtil.isNotNull(list.get(i).getImageUrl3()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl3())) {
                    list.get(i).setImageUrl3(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl3());
                }
                if (ValidateUtil.isNotNull(list.get(i).getImageUrl4()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl4())) {
                    list.get(i).setImageUrl4(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl4());
                }
                CityPostmanVerifyVO vo = new CityPostmanVerifyVO();
                BeanUtils.copyProperties(list.get(i), vo);
                if (ValidateUtil.isAbsInteger(vo.getType())) {
                    PostTypeInfo typeInfo = PostTypeMemory.getInstance().getByCode(vo.getType());
                    if (typeInfo != null) {
                        vo.setPostTypeName(typeInfo.getName());
                    }
                }
                voList.add(vo);
            }
            voList = voList.stream().map(bean -> dealPostManDepositMoney(bean)).collect(Collectors.toList());
            return AjaxResult.successResult(voList, totalCounts);
//            return AjaxResult.successResult(totalCounts, list.size(), list);
        }
        return AjaxResult.successResult(0, 0, null);
    }

    // 审核退回列表 管理员  老版本
//    @RequestMapping(method = RequestMethod.POST, value = "/admin/postman/verify/back/list")
//    @ResponseBody
//    public String verify_back_list(String token, String city, String username, Long startTime, Long endTime, Long agentId,
//                                   String province, String county, Integer page, Integer size) {
//        //验证用户有效性
//        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
//        if (user == null) {
//            return AjaxResult.errorLogoutResult();
//        }
//
//        if (!User.hasAdminAuth(user.getUserType())) {
//            return AjaxResult.errorResult("您没有操作权限");
//        }
//
//        if (ValidateUtil.isNotAbsInteger(page)) {
//            page = 1;
//        }
//        if (ValidateUtil.isNotAbsInteger(size)) {
//            size = 10;
//        }
//
//        QueryCondition query = QueryCondition.getInstance();
//        Expression expression = Expression.getInstance();
//        expression.addChild("delFlag", 0);//所有未删除的
//        expression.addChild("isVerify", 2);//未审核
//        if (ValidateUtil.isNotNull(city)) {
//            expression.addChild("city", city);
//        }
//        if (ValidateUtil.isNotNull(province)) {
//            expression.addChild("province", province);
//        }
//        if (ValidateUtil.isNotNull(county)) {
//            expression.addChild("county", county);
//        }
//        if (null != agentId) {
//            expression.addChild("region", "=", agentId);
//        }
//        if (ValidateUtil.isNotNull(username)) {
//            expression.addChild("username", "like", username);
//        }
//        if (null != startTime) {
//            expression.addChild("createTime", ">=", startTime);
//        }
//        if (null != endTime) {
//            expression.addChild("createTime", "<=", endTime);
//        }
//
//        if (0 == UserType.BUSINESS_MAN - user.getUserType()) {
//            expression.addChild("businessUsername", user.getUsername());
//        }
//
//        expression.addChild("verifyStatus", POSTMAN_VERIFY_STATUS.BACK);
//
//        query.addCondition(expression);
//        query.addOrders("createTime", "desc");
//        query.setPage(page);
//        query.setSize(size);
//
//        String sql = DaoUtils.getSql(PojoTables.POSTMAN_VERITY, query);
//        String sqlCount = DaoUtils.getSqlCount(PojoTables.POSTMAN_VERITY, query);
//        List<CityPostmanVerifyVO> list = null;
//        Long totalCount = 0l;
//        try {
//            list = (List<CityPostmanVerifyVO>) UserDaoSql.getInstance().list(CityPostmanVerifyVO.class, sql);
//            totalCount = UserDaoSql.getInstance().getCount(sqlCount);
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return AjaxResult.errorResult("获取失败，请稍后再试");
//        }
////        if (ValidateUtil.isNotAbsInteger(page)) {
////            page = 1;
////        }
////        if (ValidateUtil.isNotAbsInteger(size)) {
////            size = 20;
////        }
////        JYPageUtils.PageList pageList = JYPageUtils.getList(list, page, size);
////        if (null != pageList && null != pageList.list && pageList.list.size() > 0) {
////            List<CityPostmanVerifyVO> voList = new ArrayList<CityPostmanVerifyVO>();
////            for (int i = 0; i < pageList.list.size(); i++) {
////                if (null == pageList.list.get(i)) {
////                    continue;
////                }
////                CityPostmanVerifyVO info = new CityPostmanVerifyVO();
////                try {
////                    ObjectUtils.copyPropertiesExclude(pageList.list.get(i), info, null);
////                } catch (Exception e) {
////                    e.printStackTrace();
////                    return AjaxResult.errorResult("获取失败，请稍后再试");
////                }
////                if (null == info) {
////                    continue;
////                }
////                if (ValidateUtil.isNotNull(info.getImageUrl()) && ValidateUtil.isNotNetUrl(info.getImageUrl())) {
////                    info.setImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl());
////                }
////                if (ValidateUtil.isNotNull(info.getImageUrl1()) && ValidateUtil.isNotNetUrl(info.getImageUrl1())) {
////                    info.setImageUrl1(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl1());
////                }
////                if (ValidateUtil.isNotNull(info.getImageUrl2()) && ValidateUtil.isNotNetUrl(info.getImageUrl2())) {
////                    info.setImageUrl2(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl2());
////                }
////                if (ValidateUtil.isNotNull(info.getImageUrl3()) && ValidateUtil.isNotNetUrl(info.getImageUrl3())) {
////                    info.setImageUrl3(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl3());
////                }
////                if (ValidateUtil.isNotNull(info.getImageUrl4()) && ValidateUtil.isNotNetUrl(info.getImageUrl4())) {
////                    info.setImageUrl4(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl4());
////                }
////
////                voList.add(info);
////            }
////            if (voList.size() > 0) {
////                return AjaxResult.successResult(pageList.totalCount, voList.size(), voList);
////            }
////        }
//
//        if (ValidateUtil.isAbsList(list)) {
//            for (int i = 0; i < list.size(); i++) {
//                if (null == list.get(i)) {
//                    continue;
//                }
//                if (ValidateUtil.isNotNull(list.get(i).getImageUrl()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl())) {
//                    list.get(i).setImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl());
//                }
//                if (ValidateUtil.isNotNull(list.get(i).getImageUrl1()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl1())) {
//                    list.get(i).setImageUrl1(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl1());
//                }
//                if (ValidateUtil.isNotNull(list.get(i).getImageUrl2()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl2())) {
//                    list.get(i).setImageUrl2(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl2());
//                }
//                if (ValidateUtil.isNotNull(list.get(i).getImageUrl3()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl3())) {
//                    list.get(i).setImageUrl3(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl3());
//                }
//                if (ValidateUtil.isNotNull(list.get(i).getImageUrl4()) && ValidateUtil.isNotNetUrl(list.get(i).getImageUrl4())) {
//                    list.get(i).setImageUrl4(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getImageUrl4());
//                }
//
//            }
//
//            return AjaxResult.successResult(totalCount, list.size(), list);
//        }
//
//        return AjaxResult.successResult(0, 0, null);
//    }

    // 审核退回记录  管理员
    @RequestMapping(method = RequestMethod.POST, value = "/admin/postman/verify/back/history")
    @ResponseBody
    public String verify_back_history(String token, CityPostmanVerifyLog info) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }


        QueryCondition query = QueryCondition.getInstance();
        Expression expression = Expression.getInstance();
        expression.addChild("delFlag", 0);//所有未删除的
        expression.addChild("pId", info.getpId());//未审核

        query.addCondition(expression);
        query.addOrders("createTime", "desc");

        String sql = DaoUtils.getSql(PojoTables.POSTMAN_VERITY_LOG, query);
        List<CityPostmanVerifyLog> list = null;
        try {
            list = (List<CityPostmanVerifyLog>) UserDaoSql.getInstance().list(CityPostmanVerifyLog.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("获取失败，请稍后再试");
        }

        return AjaxResult.successResult(list.size(), list);
    }


    // 获取审核人信息(退回和通过)   通用
    @RequestMapping(method = RequestMethod.POST, value = "/postman/verify/info")
    @ResponseBody
    public String user_verify_back_info(String token, String username) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
//        if (user == null) {
//            return AjaxResult.errorLogoutResult();
//        }


//        CityPostmanVerifyVO info = null;
//        if (ValidateUtil.isNotNull(username)) {
//            info = getPostmanInfo(username);
//        } else {
//            info = getPostmanInfo(user.getUsername());
//        }
        if (ValidateUtil.isNull(username)) {
            username = user.getUsername();
        }
        CityPostmanVerifyVO info = getPostmanInfo(username);
        if (null != info) {
            return AjaxResult.successResult(info);
        }
        return AjaxResult.errorResult("未查询到数据");
    }

    // 获取锁匠的审核信息
    private CityPostmanVerifyVO getPostmanInfo(String username) {
        if (ValidateUtil.isNull(username)) {
            return null;
        }
        CityPostmanVerifyVO info = null;

        StringBuilder sql = new StringBuilder();
        sql.append("SELECT a.*, b.* FROM ").append(PojoTables.POSTMAN_VERITY)
                .append(" a  LEFT JOIN ").append(PojoTables.POSTMAN_VERITY_LOG).append(" b ON a.id = b.pId")
                .append(" WHERE a.username = '").append(username).append("'")
                .append(" AND a.delFlag = 0")
//                .append(" AND a.verifyStatus = ").append(POSTMAN_VERIFY_STATUS.BACK)
                .append(" ORDER BY b.createtime DESC")
                .append(" LIMIT 0,1");
        try {
            info = (CityPostmanVerifyVO) UserDaoSql.getInstance().getObject(CityPostmanVerifyVO.class, sql.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        if (null == info) {
            info = new CityPostmanVerifyVO();
            UserInfo userInfo = User.getUserInfoByUsername(username);
            if (null == userInfo) {
                return null;
            }
            info.setName(userInfo.getName());
            info.setTelephone(userInfo.getTelPhone());
            CityPostman postman = CityPostmanMemory.getInstance().getPostman(username);
            if (null != postman) {
                info.setProvince(postman.getProvince());
                info.setCity(postman.getCity());
                info.setCounty(postman.getCounty());
                // 如果获取不到信息的时候 就会直接获取postman中的认证信息
                info.setIsVerify(postman.getIsVerify());
            }
            // // 如果获取不到信息的时候 就会直接获取postman中的认证信息
            // info.setIsVerify(postman.getIsVerify());
            return info;
        }

        if (ValidateUtil.isNotNull(info.getImageUrl()) && ValidateUtil.isNotNetUrl(info.getImageUrl())) {
            info.setImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl());
        }
        if (ValidateUtil.isNotNull(info.getImageUrl1()) && ValidateUtil.isNotNetUrl(info.getImageUrl1())) {
            info.setImageUrl1(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl1());
        }
        if (ValidateUtil.isNotNull(info.getImageUrl2()) && ValidateUtil.isNotNetUrl(info.getImageUrl2())) {
            info.setImageUrl2(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl2());
        }
        if (ValidateUtil.isNotNull(info.getImageUrl3()) && ValidateUtil.isNotNetUrl(info.getImageUrl3())) {
            info.setImageUrl3(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl3());
        }
        if (ValidateUtil.isNotNull(info.getImageUrl4()) && ValidateUtil.isNotNetUrl(info.getImageUrl4())) {
            info.setImageUrl4(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl4());
        }
        if (ValidateUtil.isNotNull(info.getImageUrl5()) && ValidateUtil.isNotNetUrl(info.getImageUrl5())) {
            info.setImageUrl5(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl5());
        }
        if (ValidateUtil.isNotNull(info.getImageUrl6()) && ValidateUtil.isNotNetUrl(info.getImageUrl6())) {
            info.setImageUrl6(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl6());
        }
        if (ValidateUtil.isNotNull(info.getImageUrl7()) && ValidateUtil.isNotNetUrl(info.getImageUrl7())) {
            info.setImageUrl7(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl7());
        }
        if (ValidateUtil.isNotNull(info.getImageUrl8()) && ValidateUtil.isNotNetUrl(info.getImageUrl8())) {
            info.setImageUrl8(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl8());
        }
        if (ValidateUtil.isNotNull(info.getImageUrl9()) && ValidateUtil.isNotNetUrl(info.getImageUrl9())) {
            info.setImageUrl9(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl9());
        }
        if (ValidateUtil.isNotNull(info.getImageUrl10()) && ValidateUtil.isNotNetUrl(info.getImageUrl10())) {
            info.setImageUrl10(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl10());
        }
        if (ValidateUtil.isNotNull(info.getImageUrl11()) && ValidateUtil.isNotNetUrl(info.getImageUrl11())) {
            info.setImageUrl11(SYS_CONFIG_PATH.IMAGE_BASE_URL + info.getImageUrl11());
        }
        return info;
    }

    //修改postman_verify表的Extra3字段
    @RequestMapping(method = RequestMethod.POST, value = "/postman/verify/update")
    @ResponseBody
    private String updataExtra3(String token, Long id, String extra3) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }
        if (ValidateUtil.isNull(extra3)) {
            return AjaxResult.errorResult("参数不能为空！");
        }
        CityPostmanVerify obj = null;
        try {
            obj = (CityPostmanVerify) UserDaoSql.getInstance().getObject(CityPostmanVerify.class, "postman_verify", id, true);
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("数据库读取数据失败");
        }
        if (obj == null) {
            return AjaxResult.errorResult("数据为空");
        }
        StringBuffer sb = new StringBuffer();
        sb.append(" update ").append(PojoTables.POSTMAN_VERITY)
                .append(" set extra3='")
                .append(extra3)
                .append("' where id= ").append(id);
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改字段失败");
        }
        return AjaxResult.successResult("备注修改成功");
    }

    // 处理骑手押金问题
    private static CityPostmanVerifyVO dealPostManDepositMoney(CityPostmanVerifyVO vo) {
        if (vo != null && StringUtils.isNotEmpty(vo.getUsername())) {
            UserWallet wallet = UserWalletMemory.getInstance().getWallet(vo.getUsername());
            if (wallet != null) {
                vo.setDepositMoney(vo.getDepositMoney() == null ? 0.0d : vo.getDepositMoney());
            }
        }
        return vo;
    }

    // 骑手端-获取资质信息
    @RequestMapping("/postman/self/qualifies/get")
    @ResponseBody
    public String getPostmanSelfQualifies(String token) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(user.getUsername());
        if (cityPostman == null) {
            return AjaxResult.errorResult("未获取到骑手信息");
        }
        CityPostmanVerifyVO verifyVO = findPostmanQualifies(cityPostman, user.getUsername(), false);
        if (verifyVO != null) {
            return AjaxResult.successResult(verifyVO);
        }
        return AjaxResult.successResult();
    }

    // 管理后台-获取骑手的资质信息
    @RequestMapping("/city/postman/qualifies/get")
    @ResponseBody
    public String getPostmanQualifies(String token, String username) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(username);
        if (cityPostman == null) {
            return AjaxResult.errorResult("未获取到骑手信息");
        }
        CityPostmanVerifyVO verifyVO = findPostmanQualifies(cityPostman, username, true);
        if (verifyVO != null) {
            return AjaxResult.successResult(verifyVO);
        }
        return AjaxResult.successResult();
    }

    /**
     * 获取骑手的资质信息
     *
     * @param cityPostman
     * @param postmanUsername
     * @param emptyCreateDefault
     * @return
     */
    private CityPostmanVerifyVO findPostmanQualifies(CityPostman cityPostman, String postmanUsername, boolean emptyCreateDefault) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select * from ").append(PojoTables.POSTMAN_VERITY)
                .append(" where delFlag = 0 and  username ='").append(postmanUsername).append("' limit 0,1 ");
        CityPostmanVerify cityPostmanVerify = null;
        try {
            cityPostmanVerify = (CityPostmanVerify) UserDaoSql.getInstance().getObject(CityPostmanVerify.class, sql.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if (cityPostmanVerify == null && emptyCreateDefault) {
            Long time = DateUtils.getCurrTime();
            cityPostmanVerify = new CityPostmanVerify();
            cityPostmanVerify.setIsVerify(1);
            cityPostmanVerify.setVerifyStatus(POSTMAN_VERIFY_STATUS.OVER);
            cityPostmanVerify.setUsername(postmanUsername);
            cityPostmanVerify.setSex(1);
            cityPostmanVerify.setType(cityPostman.getType());
            cityPostmanVerify.setCreateTime(time);
            cityPostmanVerify.setDelFlag(0);
            cityPostmanVerify.setProvince(cityPostman.getProvince());
            cityPostmanVerify.setCity(cityPostman.getCity());
            cityPostmanVerify.setCounty(cityPostman.getCounty());
            cityPostmanVerify.setCreateUser(postmanUsername);
            try {
                UserDaoSql.getInstance().saveObj(PojoTables.POSTMAN_VERITY, cityPostmanVerify);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                cityPostmanVerify = (CityPostmanVerify) UserDaoSql.getInstance().getObject(CityPostmanVerify.class, sql.toString());
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        if (cityPostmanVerify != null) {
            CityPostmanVerifyVO verifyVO = new CityPostmanVerifyVO();
            BeanUtils.copyProperties(cityPostmanVerify, verifyVO);
            if (ValidateUtil.isNotNull(cityPostmanVerify.getImageUrl()) && ValidateUtil.isNotNetUrl(cityPostmanVerify.getImageUrl())) {
                verifyVO.setImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + cityPostmanVerify.getImageUrl());
            }
            if (ValidateUtil.isNotNull(cityPostmanVerify.getImageUrl1()) && ValidateUtil.isNotNetUrl(cityPostmanVerify.getImageUrl1())) {
                verifyVO.setImageUrl1(SYS_CONFIG_PATH.IMAGE_BASE_URL + cityPostmanVerify.getImageUrl1());
            }
            if (ValidateUtil.isNotNull(cityPostmanVerify.getImageUrl2()) && ValidateUtil.isNotNetUrl(cityPostmanVerify.getImageUrl2())) {
                verifyVO.setImageUrl2(SYS_CONFIG_PATH.IMAGE_BASE_URL + cityPostmanVerify.getImageUrl2());
            }
            if (ValidateUtil.isNotNull(cityPostmanVerify.getImageUrl3()) && ValidateUtil.isNotNetUrl(cityPostmanVerify.getImageUrl3())) {
                verifyVO.setImageUrl3(SYS_CONFIG_PATH.IMAGE_BASE_URL + cityPostmanVerify.getImageUrl3());
            }
            if (ValidateUtil.isNotNull(cityPostmanVerify.getImageUrl4()) && ValidateUtil.isNotNetUrl(cityPostmanVerify.getImageUrl4())) {
                verifyVO.setImageUrl4(SYS_CONFIG_PATH.IMAGE_BASE_URL + cityPostmanVerify.getImageUrl4());
            }
            if (ValidateUtil.isNotNull(cityPostmanVerify.getImageUrl5()) && ValidateUtil.isNotNetUrl(cityPostmanVerify.getImageUrl5())) {
                verifyVO.setImageUrl5(SYS_CONFIG_PATH.IMAGE_BASE_URL + cityPostmanVerify.getImageUrl5());
            }
            if (ValidateUtil.isNotNull(cityPostmanVerify.getImageUrl6()) && ValidateUtil.isNotNetUrl(cityPostmanVerify.getImageUrl6())) {
                verifyVO.setImageUrl6(SYS_CONFIG_PATH.IMAGE_BASE_URL + cityPostmanVerify.getImageUrl6());
            }
            if (ValidateUtil.isNotNull(cityPostmanVerify.getImageUrl7()) && ValidateUtil.isNotNetUrl(cityPostmanVerify.getImageUrl7())) {
                verifyVO.setImageUrl7(SYS_CONFIG_PATH.IMAGE_BASE_URL + cityPostmanVerify.getImageUrl7());
            }
            if (ValidateUtil.isNotNull(cityPostmanVerify.getImageUrl8()) && ValidateUtil.isNotNetUrl(cityPostmanVerify.getImageUrl8())) {
                verifyVO.setImageUrl8(SYS_CONFIG_PATH.IMAGE_BASE_URL + cityPostmanVerify.getImageUrl8());
            }
            if (ValidateUtil.isNotNull(cityPostmanVerify.getImageUrl9()) && ValidateUtil.isNotNetUrl(cityPostmanVerify.getImageUrl9())) {
                verifyVO.setImageUrl9(SYS_CONFIG_PATH.IMAGE_BASE_URL + cityPostmanVerify.getImageUrl9());
            }
            if (ValidateUtil.isNotNull(cityPostmanVerify.getImageUrl10()) && ValidateUtil.isNotNetUrl(cityPostmanVerify.getImageUrl10())) {
                verifyVO.setImageUrl10(SYS_CONFIG_PATH.IMAGE_BASE_URL + cityPostmanVerify.getImageUrl10());
            }
            if (ValidateUtil.isNotNull(cityPostmanVerify.getImageUrl11()) && ValidateUtil.isNotNetUrl(cityPostmanVerify.getImageUrl11())) {
                verifyVO.setImageUrl11(SYS_CONFIG_PATH.IMAGE_BASE_URL + cityPostmanVerify.getImageUrl11());
            }
            return verifyVO;
        }
        return null;
    }

    //  管理后台- 修改骑手资质信息
    @RequestMapping("/city/postman/qualifies/modify")
    @ResponseBody
    public String cityPostmanQualifiesModify(String token, CityPostmanVerify cityPostmanVerify, FileUploadInfo fileUploadInfo) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(cityPostmanVerify.getUsername());
        if (cityPostman == null) {
            return AjaxResult.errorResult("未获取到骑手信息");
        }
        try {
            PostmanMan.updatePostmanVerify(user, cityPostmanVerify, fileUploadInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.successResult();
    }
}
